package com.rollcall.wisdomroll;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.TextView;

public class NavigationActivity extends AppCompatActivity {

    private TextView mTextMessage;
    private FragmentTransaction transaction;
    private FragmentManager fragmentManager;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            fragmentManager = getSupportFragmentManager();  //使用fragmentmanager和transaction来实现切换效果
            transaction = fragmentManager.beginTransaction();
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    transaction.replace(R.id.content,new homeActivity());//对应的home
                    transaction.commit();  //一定不要忘记commit，否则不会显示
                    return true;
                case R.id.navigation_query:
                  transaction.replace(R.id.content,new queryActivity());
                  transaction.commit();
                    return true;
                case R.id.navigation_settings:
                    transaction.replace(R.id.content,new settingsActivity());
                    transaction.commit();
                    return true;
                case R.id.navigation_my:
                    transaction.replace(R.id.content,new myActivity());
                    transaction.commit();
                    return true;
            }
            return false;
        }
    };
    // 设置默认进来是tab 显示的页面
    private void setDefaultFragment(){
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.content,new homeActivity());
        transaction.commit();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);

        setDefaultFragment();  //上面写的那个函数

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {//解决Android 按返回键再次进入是B界面不是A界面

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }else {
            return super.onKeyDown(keyCode, event);
        }
    }


}
